﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace ManageStore
{
    public class AutoCompleteComboBox2 : System.Windows.Forms.ComboBox
    {
        public event System.ComponentModel.CancelEventHandler NotInList;

        private bool _limitToList = true;
        private bool _inEditMode = false;

        public AutoCompleteComboBox2()
            : base()
        {
        }

        [Category("Behavior")]
        public bool LimitToList
        {
            get { return _limitToList; }
            set { _limitToList = value; }
        }

        protected virtual void OnNotInList(System.ComponentModel.CancelEventArgs e)
        {
            if (NotInList != null)
            {
                NotInList(this, e);
            }
        }

        protected override void OnTextChanged(System.EventArgs e)
        {
            if (_inEditMode)
            {
                string input = Text;
                int index = FindString(input);

                if (index >= 0)
                {
                    _inEditMode = false;
                    SelectedIndex = index;
                    _inEditMode = true;
                    Select(input.Length, Text.Length);
                    this.DroppedDown = true;
                }
            }

            base.OnTextChanged(e);
        }

        protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
        {
            if (this.LimitToList)
            {
                int pos = this.FindStringExact(this.Text);

                if (pos == -1)
                {
                    OnNotInList(e);
                }
                else
                {
                    this.SelectedIndex = pos;
                }
            }

            base.OnValidating(e);
        }

        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            _inEditMode = (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);
            base.OnKeyDown(e);
        }
    }

    
    
    public class AutoCompleteComboBox : ComboBox
    {
        public AutoCompleteComboBox()
            : base()
        {
            if (this.Items != null)
            {
                foreach (object item in this.Items)
                {
                    _OriginalItems.Add(item);
                }
            }
        }

        private ObjectCollection _OriginalItems;

        public ObjectCollection OriginalItems
        {
            get { return _OriginalItems; }
            set { _OriginalItems = value; }
        }
                
     private bool controlKey = false;

        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (e.KeyChar == (int)Keys.Escape)
            {
                this.SelectedIndex = -1;
                this.Text = "";
                controlKey = true;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                controlKey = true;
            }
            else
            {
                controlKey = false;
            }
        }

        protected override void OnTextChanged(System.EventArgs e)
        {
            base.OnTextChanged(e);
            if (this.Text != "" && !controlKey)
            {
                string matchText = this.Text;
                List<string> lstSource = new List<string>();
                for (int i = 0; i < this.Items.Count; i++)
                {
                    string item = this.Items[i].ToString();
                    if (item.Contains(matchText))
                    {
                        lstSource.Add(item);
                        //match = i;
                        //break;
                    }
                }
                if (lstSource.Count > 0)
                {
                    this.Items.Clear();
                    this.Items.AddRange(lstSource.ToArray());
                    //this.DroppedDown = true;
                }
                int match = this.FindString(matchText);
                if (match != -1)
                {
                    this.SelectedIndex = match;
                    this.SelectionStart = matchText.Length;
                    this.SelectionLength = this.Text.Length - this.SelectionStart;
                }
            }
        }
    }
}